import { checkIsLogin } from "@/utils/loginStatus";
import React, { useCallback } from "react";

interface PressProps {
  onPress?: () => void;
}

export default function withPressWhenLoggedIn<T extends PressProps = PressProps>(
  WrappedComponent: React.ComponentType<T>
) {
  const displayName =
    WrappedComponent.displayName || WrappedComponent.name || "Component";
  const ComponentWithLoggedIn: React.FC<T> = ({ onPress, ...props }: T) => {
    const handlePress = useCallback(() => {
      if (checkIsLogin()) {
        onPress && onPress();
      }
    }, [onPress]);
    return <WrappedComponent {...(props as T)} onPress={handlePress} />;
  };

  ComponentWithLoggedIn.displayName = `withPressWhenLoggedIn(${displayName})`;

  return ComponentWithLoggedIn;
}